package com.csyaonie.android.storage;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.csyaonie.android.R;

public class SharedPreferencesActivity extends AppCompatActivity {

    private EditText sEditText;
    private Button sSave;
    private Button sShow;
    private TextView sTextView;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shared_preferences);

        sEditText=findViewById(R.id.sEditText);
        sSave=findViewById(R.id.sSave);
        sShow=findViewById(R.id.sShow);
        sTextView=findViewById(R.id.sTextView);

        sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
        editor=sharedPreferences.edit();

        sSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editor.putString("name",sEditText.getText().toString());
                editor.apply();
            }
        });

        sShow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sTextView.setText(sharedPreferences.getString("name",""));
            }
        });

    }
}